<!--
 * @Descripttion: 部门管理
 * @version: 
 * @Author: 许国辉
 * @Date: 2022-09-13 22:51:24
 * @LastEditors: 许国辉
 * @LastEditTime: 2022-09-13 22:52:45
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate"> 新增部门 </a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                icon: 'clarity:note-edit-line',
                label: '修改',
                onClick: handleEdit.bind(null, record),
              },
              {
                icon: 'ant-design:delete-outlined',
                label: '删除',
                color: 'error',
                popConfirm: {
                  title: '是否确认删除',
                  placement: 'left',
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <DeptModal @register="registerModal" @success="handleSuccess" />
  </div>
</template>

<script lang="ts">
  import { defineComponent, toRaw } from 'vue';
  import { getDepartmentListResultModel, DelDepartment } from '/@/api/baseInfo/departmentInfoAPI';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import DeptModal from './DeptModal.vue';
  import { columns, searchFormSchema } from './tableDepData';

  // import { useRouter } from 'vue-router';
  export default defineComponent({
    name: 'DeptManagement',
    components: { BasicTable, DeptModal, TableAction },
    setup() {
      const [registerModal, { openModal }] = useModal();
      const [registerTable, { reload }] = useTable({
        title: '部门列表',
        api: getDepartmentListResultModel,
        columns,
        pagination: { pageSize: 10 },
        size: 'small',
        formConfig: { labelWidth: 120, schemas: searchFormSchema },
        // pagination: false,
        striped: true,
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        showIndexColumn: true,
        canResize: false,
        tableSetting: { fullScreen: true },

        actionColumn: {
          width: 150,
          title: '操作',
          dataIndex: 'action', // slots: { customRender: 'action' },
          fixed: 'right',
        },
      });
      function handleCreate() {
        openModal(true, { isUpdate: false });
      }
      function handleEdit(record: Recordable) {
        openModal(true, { record, isUpdate: true });
      }
      async function handleDelete(record: Recordable) {
        // console.log(toRaw(record));
        await DelDepartment(toRaw(record).id);

        handleSuccess();
      }
      function handleSuccess() {
        reload();
      }
      return {
        registerTable,
        registerModal,
        handleCreate,
        handleEdit,
        handleDelete,
        handleSuccess,
      };
    },
  });
</script>
